<template>
  <el-dialog
    v-model="isShow"
    :title="handle.name + name"
    width="640"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <el-form class="dialog-form" label-width="100px">
      <el-row :gutter="20">
        <el-col :xs="24">
          <el-form-item label="公会名称：" required>
            <el-input v-model="form.guildName" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :xs="24">
          <el-form-item label="所属城市：" required>
            <el-cascader v-model="areaCode" :options="optionData.areaList" :props="areaProps" @change="changeArea" style="width:100%;"/>
          </el-form-item>
        </el-col>
        <el-col :xs="24" v-if="handle.type == 'add'">
          <el-form-item label="会长：" required>
            <!-- <el-select v-model="form.leaderId" placeholder="请选择" clearable style="width:100%">
               <el-option lable="xxx" value="1"></el-option>
            </el-select> -->
            <el-input
              v-model="leaderName"
              readonly
              placeholder="请选择"
              @click="refMySelect.init()"
            >
              <template #append><span @click="refMySelect.init()">选择</span></template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24">
          <el-form-item label="描述：">
            <el-input v-model="form.description" type="textarea" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <!-- <el-col :xs="24">
          <el-form-item label="状态：">
            <el-radio-group v-model="form.status">
              <el-radio v-for="(item,index) in optionData.baseType" :key="index" :label="item.id">{{item.name}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col> -->
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" :loading="btnLoading" @click="toSubmit">确定</el-button>
      </span>
    </template>
    <my-select ref="refMySelect" @confirm="getLeader"/>
  </el-dialog>
</template>

<script setup lang="ts">
import {ElMessage, ElMessageBox} from 'element-plus'
import { ref, reactive, getCurrentInstance, nextTick } from 'vue';
import { useAddEdit } from '/@/assets/compositionFn/addEdit';
const MySelect = defineAsyncComponent(() => import('./MySelect.vue'));
const refMySelect = ref();
const obj = {
  api: '/admin/guilds',
  initForm:{
    guildId: null,
    guildName: null,
    description: null,
    leaderId: null,
    pid: 0,
    adcode: null,
  },
};
let {isShow, btnLoading, handle, optionData, form, init, returnInfo, handleClose, submitForm} = useAddEdit(obj);
const emit = defineEmits(['confirm']);
const {proxy} :any = getCurrentInstance();
let areaProps = reactive({
  label: 'name',
  value: 'id'
});
let areaCode = ref([])
let leaderName = ref();
const prop = defineProps({
  name: {
    type: String,
  }
});
watch(isShow,(newVal, oldVal) => {
  if(newVal){
    //弹窗后的其他操作
    areaCode.value = [];
    let info = returnInfo();
    if(handle.type == 'edit'){
      areaCode.value = [info.adcode.substring(0,2) + '0000', info.adcode];
    }
  }else{
    areaCode.value = [];
  }
});
const getLeader = (data) => {
  form.leaderId = data.userId;
  leaderName.value = data.username;
}
const changeArea = (val) => {
  console.log(val);
  form.adcode = val[1];
}
const verifyForm = ()=>{
  if(!proxy.$verify.null(form.guildName, '请填写公会名称')){
    return false;
  }
  // if(!proxy.$verify.null(form.leaderId, '请选择会长')){
  //   return false;
  // }
  return true;
};
const toSubmit = ()=>{
  if(!verifyForm()){
    return;
  }
  submitForm().then(res => {
    emit('confirm');
  });
  
}
defineExpose({
  init,
})
</script>

<style>

</style>